package org.jinxing.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinxing.dao.BooksClassfyDAO;
import org.jinxing.dao.BooksInfoDAO;
import org.jinxing.model.ActionsInfo;
import org.jinxing.model.BooksClassfy;
import org.jinxing.model.BooksClassfyExample;
import org.jinxing.model.RolesInfo;
import org.jinxing.model.UsersInfo;
import org.jinxing.service.BooksService;
import org.jinxing.utils.JsonUtils;
import org.jinxing.utils.ModelUtils;
import org.jinxing.utils.RequestUtils;
import org.jinxing.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/bclass")
public class BookClassfyController {
	protected static String className = BookClassfyController.class.getName();
	protected static Log logger = LogFactory.getLog(className);//日志对象
	
	@Autowired
	BooksInfoDAO booksInfoDAO;
	@Autowired
	BooksClassfyDAO booksClassfyDAO;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response,Model model){
		logger.info("<|>"+className+"<|>index<|><|><|>");
		int pid = RequestUtils.getInt(request, "pid", 0);
		model.addAttribute("pid", pid);
		return "/bclass/index";
	}
	
	@RequestMapping("/getBooksClassfyes")
	public @ResponseBody String getRoles(HttpServletRequest request,HttpServletResponse response){
		logger.info("<|>"+className+"<|>getBooksClassfyes<|><|><|>");
		UsersInfo lgUser = ServletUtils.getLgUser(request);
		int pid = RequestUtils.getInt(request, "pid", 0);
		int pageNo = RequestUtils.getInt(request, "page", 1);
		int pageSize = RequestUtils.getInt(request, "pagesize", 0);
		
		BooksClassfyExample example = new BooksClassfyExample();
		example.createCriteria().andPidEqualTo(pid);
		
		if(pageSize > 0){
			example.setLimitClauseStart((pageNo-1)*pageSize);
			example.setLimitClauseCount(pageSize);
		}else{
			example.setLimitClauseStart(0);
			example.setLimitClauseCount(10);
		}
		
		List<BooksClassfy> list = this.booksClassfyDAO.selectByExample(example);
		int count = list.size();
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("Rows", list);
		result.put("Total", count);
		return JsonUtils.jsonFromObject(result);
	}
	
	@RequestMapping("/reParent")
	public String reParent(HttpServletRequest request,HttpServletResponse response,Model model){
		logger.info("<|>"+className+"<|>reParent<|><|><|>");
		int pid = RequestUtils.getInt(request, "pid", 0);
		if(0 == pid){
			model.addAttribute("pid", pid);
		}else{
			BooksClassfy classfy = this.booksClassfyDAO.selectByPrimaryKey(pid);
			model.addAttribute("pid", classfy.getPid());
		}
		return "/bclass/index";
	}
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("json");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Integer> successId = new LinkedList<Integer>();
		List tempList = JsonUtils.listFromJson(json);
		for(Object obj : tempList){
			BooksClassfy classfy = ModelUtils.booksClassfyFromJson(JsonUtils.jsonFromObject(obj));
			
			if(null == classfy.getId()){
				Integer id = this.booksClassfyDAO.insert(classfy);
				if(0 < id){
					successId.add(id);
				}
			}else{
				if(this.booksClassfyDAO.updateByPrimaryKey(classfy) == 1){
					successId.add(classfy.getId());
				}
			}
		}
		result.put("sucessList", successId);
		logger.info("<|>"+className+"<|>saveOrUpdate<|><|>"+successId+"<|>");
		return JsonUtils.jsonFromObject(result);
	}
	
	
}
